﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FizbinGestureController.cs" company="arvystate.net">
//   arvystate.net
// </copyright>
// <summary>
//   Gesture controller uses Fizbin Gesture Controller to recognize gestures
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace PresentationAid.Lib.GestureControllers
{
    using Fizbin.Kinect.Gestures;

    using Kinect.Toolbox;

    using Microsoft.Kinect;

    using NLog;

    using PresentationAid.Lib.Enums;

    /// <summary>
    /// Gesture controller uses <c>Fizbin</c> Gesture Controller to recognize gestures
    /// </summary>
    public class FizbinGestureController : DelayGestureController
    {
        #region Static Fields

        /// <summary>
        /// The logger.
        /// </summary>
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

        #endregion

        #region Fields

        /// <summary>
        /// The _gesture controller.
        /// </summary>
        private readonly GestureController gestureController;

        /// <summary>
        /// The _skeletons.
        /// </summary>
        private Skeleton[] skeletons;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FizbinGestureController"/> class.
        /// </summary>
        public FizbinGestureController()
        {
            Logger.Trace("Init: FizbinGestureController");

            this.gestureController = new GestureController();
            this.gestureController.GestureRecognized += this.OnGestureRecognized;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The recognize.
        /// </summary>
        /// <param name="frame">
        /// The frame.
        /// </param>
        public override void Recognize(SkeletonFrame frame)
        {
            frame.GetSkeletons(ref this.skeletons);

            foreach (Skeleton skeleton in this.skeletons)
            {
                if (skeleton.TrackingState != SkeletonTrackingState.Tracked)
                {
                    continue;
                }

                this.gestureController.UpdateAllGestures(skeleton);
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The on gesture recognized.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnGestureRecognized(object sender, GestureEventArgs e)
        {
            Logger.Debug("Event: GestureRecognized - GestureType: {0}", e.GestureType);

            if (e.GestureType == GestureType.JoinedHands)
            {
                this.ExecuteGestureRecognized(GestureCommand.JoinedHands, e.TrackingId);
            }
            else if ((e.GestureType == GestureType.WaveLeft) || (e.GestureType == GestureType.WaveRight))
            {
                this.ExecuteGestureRecognized(GestureCommand.Wave, e.TrackingId);
            }
        }

        #endregion
    }
}